getwd()

setwd()

###############################
## INSTALL AND LOAD PACKAGES ##
###############################

#install.packages("ggforestplot")
#install.packages("ggplot2")
#install.packages("haven")
#install.packages("magrittr")
#install.packages("tidyverse")
#install.packages("sjPlot")
#install.packages("sjmisc")
#install.packages("effects")
#install.packages("psych")
#install.packages("ggeffects")
#install.packages("ggpubr")
#install.packages("grid")
#install.packages("table1")
#install.packages("flextable")
#install.packages("expss")
#install.packages("srvyr")
#install.packages("ggeasy")
#install.packages("scales")
#install.packages("gridExtra")
#install.packages("cowplot")
#install.packages("ggridges")

library(ggforestplot)
library(ggplot2)
library(ggpubr)
library(grid)
library(haven)
library(magrittr)
library(tidyverse)
library(sjPlot)
library(sjmisc)
library(effects)
library(psych)
library(ggeffects)
library(table1)
library(flextable)
library(srvyr)
library(ggeasy)
library(scales)
library(gridExtra)
library(cowplot)
library(ggridges)
library(lattice)


###############
## LOAD DATA ##
###############

merge.dataset <- sjlabelled::read_stata("merge_integrity.dta")


############################
## DESCRIPTIVE STATISTICS ##
############################

theme_update(plot.title = element_text(hjust = 0.5))

# Create binary variable for perceived election fairness

merge.dataset <- mutate(merge.dataset, integrity_2pts = case_when(
integrity_4pts == "1" | integrity_4pts == "2" ~ 1, 
integrity_4pts == "3" | integrity_4pts == "4" ~ 2,
TRUE ~ NA_real_))                

# Create binary variable for satisfaction with democracy

merge.dataset <- mutate(merge.dataset, swd_post_2pts = case_when(
swd_post == "1" | swd_post == "2" ~ 1, swd_post == "3" | swd_post == "4" ~ 2,
TRUE ~ NA_real_))

# Perceived Electoral Integrity

fair <- merge.dataset %>% drop_na(weight,integrity_4pts) %>%
as_survey(weights = c(weight)) %>%
group_by(election,integrity_4pts) %>%
summarize(n = survey_total())

fair <- fair %>%
group_by(election) %>%
mutate(percent_int = n/sum(n))

fair.g <- ggplot(fair, aes(x = factor(election), y = percent_int*100, 
fill = factor(integrity_4pts))) + geom_bar(show.legend = FALSE, 
position = position_fill(reverse = TRUE), stat="identity", width = 0.7) +
labs(x = "", y = "Percentage of Sample", fill = "integrity_4pts") + theme_bw() +
theme_minimal(base_size = 14) + 
scale_y_continuous(labels = label_percent(suffix = ""), 
breaks = breaks_width(.2)) + scale_fill_manual(name="Election Integrity", 
labels=c("Unfair","Somewhat unfair","Somewhat fair","Fair"), 
values=c('#f34a3e','#f8928b', '#02c8c5', '#029694')) + 
theme(panel.border = element_rect(colour = "black", fill=NA, linewidth=0), 
legend.key.size = unit(.5, "cm"),
axis.title.y = element_text(margin = margin(t = 0, r = -4, b = 0, l = 0)),
plot.title = element_text(size = 13, hjust = 0.5), 
legend.text = element_text(size=10), legend.title = element_text(size = 10), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11)) + 
scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016",
"US 2020")) + ggtitle("(a) Perceived Electoral Integrity")
fair.g

# Satisfaction with Democracy

swd <- merge.dataset %>% drop_na(weight,swd_post) %>%
as_survey(weights = c(weight)) %>% group_by(election,swd_post) %>%
summarize(n = survey_total())

swd <- swd %>% group_by(election) %>% mutate(percent_swd = n/sum(n))

swd.g <- ggplot(data = swd, mapping = aes(y = percent_swd*100, x = election, 
group = factor(swd_post), colour=factor(swd_post), shape=factor(swd_post))) +
geom_line(show.legend = FALSE, aes(linetype=factor(swd_post), color=factor(swd_post)), alpha = 1, size = 1) +
theme_bw() + scale_y_continuous(limits=c(0, 60), 
breaks = seq(0, 60, 10)) +
labs(x = "", y = "") +
scale_linetype_manual(name="SWD", labels=c("Not at all","Not very","Fairly","Satisfied"),values=c(3,1,1,3)) +
scale_color_manual(name="SWD", labels=c("Not at all","Not very","Fairly","Satisfied"), values=c('#f34a3e', '#f34a3e', '#029694', '#029694')) +
theme(legend.text = element_text(size=10), legend.title = element_text(size = 10),plot.title = element_text(size = 13, hjust = 0.5), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016","US 2020")) +
ggtitle("(b) Satisfaction with Democracy")
swd.g

# Confidence in Electoral Integrity by PID Status

fair.pid <- merge.dataset %>% drop_na(weight,pidstatus_whole,integrity_2pts) %>%
as_survey(weights = c(weight)) %>% group_by(election,integrity_2pts,
pidstatus_whole) %>% summarize(n = survey_total())

fair.pid <- fair.pid %>% group_by(election,pidstatus_whole) %>% 
mutate(percent_int = n/sum(n))

fair.pid <- fair.pid %>% filter(integrity_2pts==2)

fair.pid$pidstatus_whole <- as.factor(fair.pid$pidstatus_whole)

fair.pid.g <- ggplot(fair.pid, aes(x = election, y = percent_int*100)) + 
labs(x = "", y = "Percentge of sample") + geom_col(show.legend = FALSE, 
aes(color = pidstatus_whole, fill = pidstatus_whole), 
position = position_dodge(0.4), width = 0.2) +
scale_color_manual(values = c('#E69F00', '#999999', '#56B4E9')) + theme_bw() +
scale_fill_manual(values = c('#E69F00', '#999999', '#56B4E9')) + 
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
legend.key.size = unit(.5, "cm"),
axis.title.y = element_text(margin = margin(t = 0, r = -4, b = 0, l = 0)),
legend.text = element_text(size=10), 
legend.title = element_text(size = 10),
plot.title = element_text(size = 13, hjust = 0.5), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11)) + 
scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016",
"US 2020")) + ggtitle("(g) Confidence in Electoral Integrity by PID Status")
fair.pid.g






pid <- merge.dataset %>% drop_na(weight,pidstatus_whole) %>%
  as_survey(weights = c(weight)) %>%
  group_by(election,pidstatus_whole) %>%
  summarize(n = survey_total())

pid <- pid %>%
  group_by(election) %>%
  mutate(percent_pid = n/sum(n))

pid$pidstatus_whole <- as.factor(pid$pidstatus_whole)
  
vote <- merge.dataset %>% drop_na(weight,voteactstatus_whole) %>%
  as_survey(weights = c(weight)) %>%
  group_by(election,voteactstatus_whole) %>%
  summarize(n = survey_total())

vote <- vote %>%
  group_by(election) %>%
  mutate(percent_vote = n/sum(n))

vote$voteactstatus_whole <- as.factor(vote$voteactstatus_whole)
  
statusp <- merge.dataset %>% drop_na(weight,pidstatus_whole,correct_whole_d) %>%
  as_survey(weights = c(weight)) %>%
  group_by(election,correct_whole_d,pidstatus_whole) %>%
  summarize(n = survey_total())

statusp <- statusp %>%
  group_by(election,pidstatus_whole) %>%
  mutate(percent_correct = n/sum(n))

statusp <- statusp %>% filter(correct_whole_d==1)

statusp$pidstatus_whole <- as.factor(statusp$pidstatus_whole)

statuspt <- merge.dataset %>% drop_na(weight,pidstatus_whole,correct_whole_d) %>%
  as_survey(weights = c(weight)) %>%
  group_by(election,correct_whole_d) %>%
  summarize(n = survey_total())

statuspt <- statuspt %>%
  group_by(election) %>%
  mutate(percent_total = n/sum(n))

statuspt <- statuspt %>% filter(correct_whole_d==1)
statuspt = subset(statuspt, select = c(election, percent_total))

statusp = merge(x = statusp, y = statuspt, by = "election")
statusp

statusv <- merge.dataset %>% drop_na(weight,voteactstatus_whole,correct_whole_d) %>%
  as_survey(weights = c(weight)) %>%
  group_by(election,correct_whole_d,voteactstatus_whole) %>%
  summarize(n = survey_total())

statusv <- statusv %>%
  group_by(election,voteactstatus_whole) %>%
  mutate(percent_correct = n/sum(n))

statusv <- statusv %>% filter(correct_whole_d==1)

statusv$voteactstatus_whole <- as.factor(statusv$voteactstatus_whole)

statusvt <- merge.dataset %>% drop_na(weight,voteactstatus_whole,correct_whole_d) %>%
  as_survey(weights = c(weight)) %>%
  group_by(election,correct_whole_d) %>%
  summarize(n = survey_total())

statusvt <- statusvt %>%
  group_by(election) %>%
  mutate(percent_total = n/sum(n))

statusvt <- statusvt %>% filter(correct_whole_d==1)
statusvt = subset(statusvt, select = c(election, percent_total))

statusv = merge(x = statusv, y = statusvt, by = "election")
statusv


swdpid <- merge.dataset %>% drop_na(weight,swd_post,pid) %>%
  as_survey(weights = c(weight)) %>%
  group_by(election,swd_post,pid) %>%
  summarize(n = survey_total())

swdpid <- swdpid %>%
  group_by(election) %>%
  mutate(percent_swd = n/sum(n))

swdpid <- swdpid %>% filter(pid==1 | pid==2)





tiff("swdpid_g.tiff", units="in", width=12, height=5, res=300)

swdpid_g <- ggplot(data = swdpid, mapping = aes(y = percent_swd*100, x = election, 
                                            group = factor(swd_post), colour=factor(swd_post), shape=factor(swd_post))) +
  geom_line(show.legend = TRUE, aes(linetype=factor(swd_post), color=factor(swd_post)), alpha = 1, size = 1) +
  theme_bw() + scale_y_continuous(limits=c(0, 30), 
                                  breaks = seq(0, 30, 10)) +
  labs(x = "", y = "Percentage of Sample") +
  scale_linetype_manual(name="SWD", labels=c("Not at all","Not very","Fairly","Satisfied"),values=c(3,1,1,3)) +
  scale_color_manual(name="SWD", labels=c("Not at all","Not very","Fairly","Satisfied"), values=c('#F8766D', '#F8766D', '#00BFC4', '#00BFC4')) +
  scale_shape_manual(name="SWD", labels=c("Not at all","Not very","Fairly","Satisfied"),values=c(3,1,1,3)) +
  theme(strip.text = element_text(size = 12), legend.text = element_text(size=10), legend.title = element_text(size = 10),plot.title = element_text(size = 13, hjust = 0.5), 
        axis.title = element_text(size = 12), axis.text = element_text(size = 11), panel.grid.major = element_blank(), panel.grid.minor = element_blank()) + 
  scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016","US 2020")) +
  ggtitle("Satisfaction with Democracy Among Democrats and Republicans") + facet_wrap(vars(pid), labeller = labeller(pid = c("1" = "Democrats", "2" = "Republicans")))
swdpid_g

dev.off()

pid_g <- ggplot(pid, aes(x = factor(election), y = percent_pid*100, fill = factor(pidstatus_whole))) +
  geom_bar(show.legend = FALSE, position = position_fill(reverse = TRUE), stat="identity", width = 0.7) +
  labs(x = "", y = "", fill = "pidstatus_whole") + theme_bw() + labs(x = "", y = "Percentage of Sample") +
  theme_minimal(base_size = 14) + scale_y_continuous(labels = label_percent(suffix = ""), breaks = breaks_width(.2)) +
 scale_fill_manual(name="Electoral Status", labels=c("Losers","No PID/Non-voters","Winners"), values=c('#E69F00', '#999999', '#56B4E9')) + 
  theme(panel.border = element_rect(colour = "black", fill=NA, linewidth=0), legend.key.size = unit(.5, "cm"),axis.title.y = element_text(margin = margin(t = 0, r = -4, b = 0, l = 0)),plot.title = element_text(size = 13, hjust = 0.5), legend.text = element_text(size=10), legend.title = element_text(size = 10), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11)) + 
  scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016","US 2020")) +
  ggtitle("(a) Distribution of ANES Respondents by PID Status")
pid_g

vote_g <- ggplot(vote, aes(x = factor(election), y = percent_vote*100, fill = factor(voteactstatus_whole))) +
  geom_bar(show.legend = FALSE, position = position_fill(reverse = TRUE), stat="identity", width = 0.7) +
  labs(x = "", y = "", fill = "voteactstatus_whole") + theme_bw() +
  theme_minimal(base_size = 14) + scale_y_continuous(labels = label_percent(suffix = ""), breaks = breaks_width(.2)) +
 scale_fill_manual(name="Electoral Status", labels=c("Losers","No PID/Non-voters","Winners"), values=c('#E69F00', '#999999', '#56B4E9')) + 
  theme(panel.border = element_rect(colour = "black", fill=NA, linewidth=0), legend.key.size = unit(.5, "cm"),axis.title.y = element_text(margin = margin(t = 0, r = -4, b = 0, l = 0)),plot.title = element_text(size = 13, hjust = 0.5), legend.text = element_text(size=10), legend.title = element_text(size = 10), 
        axis.title = element_text(size = 12), axis.text = element_text(size = 11)) + 
  scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016","US 2020")) +
  ggtitle("(b) Distribution of ANES Respondents by Vote Status")
vote_g

p <- ggplot(statusp, aes(x = election, y = percent_correct*100)) + labs(x = "", y = "Correct Forecasts (%)") +
  geom_col(show.legend = FALSE, aes(color = pidstatus_whole, fill = pidstatus_whole), position = position_dodge(0.4), width = 0.2) +
  scale_color_manual(name="Electoral Status", labels=c("Losers","No PID/Non-voters","Winners"),values = c('#E69F00', '#999999', '#56B4E9')) + theme_bw() +
 scale_fill_manual(name="Electoral Status", labels=c("Losers","No PID/Non-voters","Winners"),values = c('#E69F00', '#999999', '#56B4E9')) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),legend.key.size = unit(.5, "cm"),axis.title.y = element_text(margin = margin(t = 0, r = -4, b = 0, l = 0)),legend.text = element_text(size=10), 
legend.title = element_text(size = 10),plot.title = element_text(size = 13, hjust = 0.5), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11)) + 
  scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016","US 2020")) +
  ggtitle("(c) Percentage of Correct Forecasts by PID Status") + geom_line(data=statusp,linewidth=1,group="election",mapping=aes(x=election,y=percent_total*100))
p

v <- ggplot(statusv, aes(x = election, y = percent_correct*100)) + labs(x = "", y = "") +
  geom_col(show.legend = FALSE, aes(color = voteactstatus_whole, fill = voteactstatus_whole), position = position_dodge(0.4), width = 0.2) +
  scale_color_manual(values = c('#E69F00', '#999999', '#56B4E9')) + theme_bw() +
 scale_fill_manual(values = c('#E69F00', '#999999', '#56B4E9')) + theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),legend.key.size = unit(.5, "cm"),axis.title.y = element_text(margin = margin(t = 0, r = -4, b = 0, l = 0)),legend.text = element_text(size=10), 
legend.title = element_text(size = 10),plot.title = element_text(size = 13, hjust = 0.5), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11)) + 
  scale_x_discrete(labels=c("US 1996","US 2000","US 2004","US 2012","US 2016","US 2020")) +
  ggtitle("(d) Percentage of Correct Forecasts by Vote Status") + geom_line(data=statusv,linewidth=1,group="election",mapping=aes(x=election,y=percent_total*100))
v



comb1 <- ggarrange(fair.g, swd_g, nrow = 1) 

ggsave(plot = comb1,filename="comb1.tiff",  
       units="in", width=10, height=5, dpi=300)

comb2 <- ggarrange(p, v, nrow = 1) 

ggsave(plot = comb2,filename="comb2.tiff",  
       units="in", width=10, height=5, dpi=300)

ds.swd <- merge.dataset %>% drop_na(caseid,suspicion,swd_post_2pts)
ds.swd = subset(ds.swd, select = c(caseid,suspicion,swd_post_2pts))

ds.int <- merge.dataset %>% drop_na(caseid,suspicion,integrity_2pts)
ds.int = subset(ds.int, select = c(caseid,suspicion,integrity_2pts))

ds.swd$swd_post_2pts <- as.factor(ds.swd$swd_post_2pts)
ds.swd$caseid <- as.factor(ds.swd$caseid)
ds.swd$suspicion <- as.numeric(ds.swd$suspicion)

ds.int$integrity_2pts <- as.factor(ds.int$integrity_2pts)
ds.int$caseid <- as.factor(ds.int$caseid)
ds.int$suspicion <- as.numeric(ds.int$suspicion)

ds.swd.g <- ggplot(ds.swd) +
  geom_density_ridges(show.legend = FALSE, color = "white", aes(x = suspicion, y = caseid,
group = interaction(swd_post_2pts,caseid),fill=swd_post_2pts,
scale = 0.8), alpha = 0.4) + ylab(NULL) + 
scale_fill_manual(name = "SWD", values = c("#8cd3ff", "#0747a1"), labels = c("Not at all + Not very", "Fairly + Satisfied")) +
theme_bw() + scale_x_continuous(limits=c(0, 1), breaks = seq(0, 1, .2)) +
panel_border(color = "grey85", size = 1, linetype = 1, remove = TRUE) +
theme(panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(),
axis.ticks.y = element_blank(), legend.text = element_text(size=10), legend.title = element_text(size = 10),plot.title = element_text(size = 13, hjust = 0.5), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11)) +
scale_y_discrete(labels=c("","","")) +
ggtitle("(f) Density Plot of Suspicion \n Index by Satisfaction with Democracy") + labs(x = "Suspicion Index", y = "")

ds.int.g <- ggplot(ds.int) +
  geom_density_ridges(show.legend = FALSE, color = "white", aes(x = suspicion, y = caseid,
group = interaction(integrity_2pts,caseid),fill=integrity_2pts,
scale = 0.8), alpha = 0.4) + ylab(NULL) + 
 scale_fill_manual(name = "Election Integrity", values = c("#ff8164", "#990000"), labels = c("Somewhat unfair + Unfair", "Somewhat fair + Fair")) +
theme_bw() + scale_x_continuous(limits=c(0, 1), breaks = seq(0, 1, .2)) +
  panel_border(color = "grey85", size = 1, linetype = 1, remove = TRUE) +
  theme(panel.grid.major.y = element_blank(),panel.grid.minor.y = element_blank(),
legend.text = element_text(size=10), legend.title = element_text(size = 10),plot.title = element_text(size = 13, hjust = 0.5), 
axis.title = element_text(size = 12), axis.text = element_text(size = 11)) +
  scale_y_discrete(labels=c("US 2012","US 2016","US 2020")) +
  ggtitle("(e) Density Plot of Suspicion \n Index by Perceived Electoral Integrity") + labs(x = "Suspicion Index", y = "")

comb3 <- ggarrange(ds.int.g, ds.swd.g, nrow = 1) 

ggsave(plot = comb3,filename="comb3.tiff",  
       units="in", width=10, height=5, dpi=300)

comb4 <- ggarrange(pid_g, vote_g, nrow = 1) 

ggsave(plot = comb4,filename="comb4.tiff",  
       units="in", width=10, height=5, dpi=300)


##########################################
## 1. FIGURE 1: UNEXPECTED–EXPECTED GAP ##
##########################################

# Load data (PID)

integrity.gap.pid <- read_dta("parmest_inter_integrity_swd_pid.dta")

View(integrity.gap.pid)

integrity.gap.pid <- integrity.gap.pid %>% mutate(comp = dplyr::recode(comp, "independents" = "nopid"))

integrity.gap.pid <- integrity.gap.pid %>% filter(!(electionid == "us1996-2004" | 
                                                      electionid == "us2012-2020"))

integrity.gap.pid <- integrity.gap.pid %>% filter(!(electionid == "us2012" & model == "base" | 
                     electionid == "us2016" & model == "base" | electionid == "us2020" & model == "base" | 
                     electionid == "us2012-2020" & model == "base"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("integrity_gap_pid_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.gap.pid.g <- ggplot(data = integrity.gap.pid, aes(x = estimate, y = electionid)) + 
                         geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = comp, shape = comp),
                         position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                         linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.gap.pid.g <- integrity.gap.pid.g + scale_colour_manual(limits=c("losers", "nopid", "winners"),values=c("#F8766D", "#619CFF", "#00BA38"), 
                         name="Electoral Status", labels=c("Losers","Independents","Winners")) +
                         scale_shape_manual(limits=c("losers", "nopid", "winners"),values=c(15,16,17), name="Electoral Status", labels=c("Losers","Independents","Winners")) +
                         theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                         limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                         "US 2012","US 2016","US 2020")) +
                         scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-1.5, 1.5), 
                         breaks = seq(-1.5, 1.5, 1.5)) + facet_wrap(vars(dv), labeller = labeller(dv = c("integrity" = "Perceived Electoral Integrity",
                         "swd" = "Satisfaction with Democracy"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                         axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
                         guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.gap.pid.g$facet$params$nrow=1

print(integrity.gap.pid.g)

dev.off()

##########################################
## 1. FIGURE 1: POST ONLY, UNEXPECTED–EXPECTED GAP ##
##########################################

# Load data (PID)

integrity.post.gap.pid <- read_dta("parmest_post_inter_integrity_swd_pid.dta")

View(integrity.post.gap.pid)

integrity.post.gap.pid <- integrity.post.gap.pid %>% mutate(comp = dplyr::recode(comp, "independents" = "nopid"))

integrity.post.gap.pid <- integrity.post.gap.pid %>% filter(!(electionid == "us1996-2004" | 
                                                      electionid == "us2012-2020"))

integrity.post.gap.pid <- integrity.post.gap.pid %>% filter(!(electionid == "us2012" & model == "base" | 
                                                      electionid == "us2016" & model == "base" | electionid == "us2020" & model == "base" | 
                                                      electionid == "us2012-2020" & model == "base"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("integrity_post_gap_pid_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.post.gap.pid.g <- ggplot(data = integrity.post.gap.pid, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = comp, shape = comp),
position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.post.gap.pid.g <- integrity.post.gap.pid.g + scale_colour_manual(limits=c("losers", "nopid", "winners"),values=c("#F8766D", "#619CFF", "#00BA38"), 
name="Electoral Status", labels=c("Losers","Independents","Winners")) +
  scale_shape_manual(limits=c("losers", "nopid", "winners"),values=c(15,16,17), name="Electoral Status", labels=c("Losers","Independents","Winners")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
"US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-1.5, 1.5), 
breaks = seq(-1.5, 1.5, 1.5)) + facet_wrap(vars(dv), labeller = labeller(dv = c("integrity" = "Perceived Electoral Integrity",
"swd" = "Satisfaction with Democracy"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.post.gap.pid.g$facet$params$nrow=1

print(integrity.post.gap.pid.g)

dev.off()


##########################################
## 1. FIGURE 1: PREPOST, UNEXPECTED–EXPECTED GAP ##
##########################################

# Load data (PID)

integrity.prepost.gap.pid <- read_dta("parmest_prepost_inter_integrity_swd_pid.dta")

View(integrity.prepost.gap.pid)

integrity.prepost.gap.pid <- integrity.prepost.gap.pid %>% mutate(comp = dplyr::recode(comp, "independents" = "nopid"))

integrity.prepost.gap.pid <- integrity.prepost.gap.pid %>% filter(!(electionid == "us1996-2004" | 
                                                                electionid == "us2012-2020"))

integrity.prepost.gap.pid <- integrity.prepost.gap.pid %>% filter(!(electionid == "us2012" & model == "base" | 
                                                                electionid == "us2016" & model == "base" | electionid == "us2020" & model == "base" | 
                                                                electionid == "us2012-2020" & model == "base"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("integrity_prepost_gap_pid_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.prepost.gap.pid.g <- ggplot(data = integrity.prepost.gap.pid, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = comp, shape = comp),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.prepost.gap.pid.g <- integrity.prepost.gap.pid.g + scale_colour_manual(limits=c("losers", "nopid", "winners"),values=c("#F8766D", "#619CFF", "#00BA38"), 
name="Electoral Status", labels=c("Losers","Independents","Winners")) +
  scale_shape_manual(limits=c("losers", "nopid", "winners"),values=c(15,16,17), name="Electoral Status", labels=c("Losers","Independents","Winners")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
"US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-1.5, 1.5), 
                     breaks = seq(-1.5, 1.5, 1.5)) + facet_wrap(vars(dv), labeller = labeller(dv = c("integrity" = "Perceived Electoral Integrity",
"swd" = "Satisfaction with Democracy"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.prepost.gap.pid.g$facet$params$nrow=1

print(integrity.prepost.gap.pid.g)

dev.off()

# Load data (Vote)

integrity.gap.vote <- read_dta("parmest_inter_integrity_swd_vote.dta")

View(integrity.gap.vote)

integrity.gap.vote <- integrity.gap.vote %>% filter(!(electionid == "us1996-2004" | 
                                                      electionid == "us2012-2020"))

integrity.gap.vote <- integrity.gap.vote %>% filter(!(electionid == "us2012" & model == "base" | 
electionid == "us2016" & model == "base" | electionid == "us2020" & model == "base" | 
electionid == "us2012-2020" & model == "base"))

# Create graph: Unexpected–Expected Gap

png("integrity_gap_vote_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.gap.vote.g <- ggplot(data = integrity.gap.vote, aes(x = estimate, y = electionid)) + 
                        geom_effect(show.legend = FALSE, ggplot2::aes(xmin = min95, xmax = max95, colour = comp, shape = comp),
                        position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                        linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.gap.vote.g <- integrity.gap.vote.g + scale_colour_manual(limits=c("losers", "nonvoters", "winners"),values=c("#F8766D", "#619CFF", "#00BA38"), 
                        name="Electoral Status", labels=c("Losers","No PID/Non-Voters","Winners")) +
                        scale_shape_manual(limits=c("losers", "nonvoters", "winners"),values=c(15,16,17), name="Electoral Status", labels=c("Losers","No PID/Non-Voters","Winners")) +
                        theme(plot.title = element_text(size = 11, hjust = 0.5), axis.ticks.y = element_blank()) + scale_y_discrete(name = "", 
                        limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                        "US 2012","US 2016","US 2020")) +
                        scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-1.5, 1.5), 
                        breaks = seq(-1.5, 1.5, 1.5)) + facet_wrap(vars(dv), labeller = labeller(dv = c("integrity" = "Perceived Electoral Integrity",
                        "swd" = "Satisfaction with Democracy"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                        axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
                        guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.gap.vote.g$facet$params$nrow=1

print(integrity.gap.vote.g)

dev.off()

gap <- ggarrange(integrity.gap.pid.g, integrity.gap.vote.g, nrow = 1) 

ggsave(plot = gap,filename="gap.png",  
       units="mm", width=400, height=150, dpi=300)


######################################################
## 2. FIGURE 2: UNEXPECTED–EXPECTED GAP (SUSPICION) ##
######################################################

# Load data (PID)

integrity.gap.3x.pid <- read_dta("parmest_inter_3x_pid.dta")

View(integrity.gap.3x.pid)

integrity.gap.3x.pid <- integrity.gap.3x.pid %>% filter(!(electionid == "us1996-2004" | 
                                                        electionid == "us2012-2020"))

integrity.gap.3x.pid <- integrity.gap.3x.pid %>% mutate(comp = recode(comp, "independents" = 'nopid'))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

tiff("integrity_gap_3x_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.gap.3x.pid.g <- ggplot(data = integrity.gap.3x.pid, aes(x = estimate, y = electionid)) + 
                          geom_effect(ggplot2::aes(xmin = min95, xmax = max95, colour = comp, shape = comp),
                          position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                          linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.gap.3x.pid.g <- integrity.gap.3x.pid.g + scale_colour_manual(limits=c("losers", "nopid", "winners"),values=c("#F8766D", "#619CFF", "#00BA38"), 
                          name="Electoral Status", labels=c("Losers","No PID/Non-Voters","Winners")) +
                          scale_shape_manual(limits=c("losers", "nopid", "winners"),values=c(15,16,17), name="Electoral Status", labels=c("Losers","No PID/Non-Voters","Winners")) +
                          theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                          limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                          "US 2012","US 2016","US 2020")) +
                          scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-1.5, 1.5), 
                          breaks = seq(-1.5, 1.5, 1.5)) + facet_wrap(vars(dv), labeller = labeller(dv = c("integrity" = "Perceived Electoral Integrity",
                          "swd" = "Satisfaction with Democracy"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                          axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
                          guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("Party Identification")

integrity.gap.3x.pid.g$facet$params$nrow=2

print(integrity.gap.3x.pid.g)

dev.off()

# Load data (Vote)

integrity.gap.3x.vote <- read_dta("parmest_inter_3x_vote.dta")

View(integrity.gap.3x.vote)

integrity.gap.3x.vote <- integrity.gap.3x.vote %>% filter(!(electionid == "us1996-2004" | 
                                                            electionid == "us2012-2020"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

tiff("integrity_gap_3x_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.gap.3x.vote.g <- ggplot(data = integrity.gap.3x.vote, aes(x = estimate, y = electionid)) + 
                           geom_effect(ggplot2::aes(xmin = min95, xmax = max95, colour = comp, shape = comp),
                           position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                           linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.gap.3x.vote.g <- integrity.gap.3x.vote.g + scale_colour_manual(limits=c("losers", "nonvoters", "winners"),values=c("#F8766D", "#619CFF", "#00BA38"), 
                           name="Electoral Status", labels=c("Losers","No PID/Non-Voters","Winners")) +
                           scale_shape_manual(limits=c("losers", "nonvoters", "winners"),values=c(15,16,17), name="Electoral Status", labels=c("Losers","No PID/Non-Voters","Winners")) +
                           theme(plot.title = element_text(size = 11, hjust = 0.5), axis.ticks.y = element_blank()) + scale_y_discrete(name = "", 
                           limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                           "US 2012","US 2016","US 2020")) +
                           scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-1.5, 1.5), 
                           breaks = seq(-1.5, 1.5, 1.5)) + facet_wrap(vars(dv), labeller = labeller(dv = c("integrity" = "Perceived Electoral Integrity",
                           "swd" = "Satisfaction with Democracy"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                           axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
                           guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("Reported Vote")

integrity.gap.3x.vote.g$facet$params$nrow=2

print(integrity.gap.3x.vote.g)

dev.off()


###################################################
## 3. FIGURE 3: THREE-WAY INTERACTION: SUSPICION ##
###################################################

# Load data

merge.dataset <- sjlabelled::read_stata("merge_integrity.dta")

merge.dataset$pidstatus_whole <- ordered(merge.dataset$pidstatus_whole,
                                           levels = c(1,2,3),
                                           labels = c("Losers", "Independents", "Winners"))

merge.dataset$voteactstatus_whole <- ordered(merge.dataset$voteactstatus_whole,
                                               levels = c(1,2,3),
                                               labels = c("Losers", "Non-Voters", "Winners"))

merge.dataset$unexpected_p <- ordered(merge.dataset$unexpected_p,
                                        levels = c(0,1),
                                        labels = c("Expected", "Unexpected"))

merge.dataset$unexpected_v <- ordered(merge.dataset$unexpected_v,
                                        levels = c(0,1),
                                        labels = c("Expected", "Unexpected"))

# Define variables as factor or numeric

merge.dataset$pidstatus_whole <- as.factor(merge.dataset$pidstatus_whole)
merge.dataset$voteactstatus_whole <- as.factor(merge.dataset$voteactstatus_whole)
merge.dataset$unexpected_p <- as.factor(merge.dataset$unexpected_p)
merge.dataset$unexpected_v <- as.factor(merge.dataset$unexpected_v)
merge.dataset$male <- as.factor(merge.dataset$male)
merge.dataset$white <- as.factor(merge.dataset$white)
merge.dataset$education <- as.factor(merge.dataset$education)
merge.dataset$knowledge_n <- as.numeric(merge.dataset$knowledge_n)
merge.dataset$income <- as.numeric(merge.dataset$income)
merge.dataset$region <- as.factor(merge.dataset$region)

merge.dataset$integrity_4pts <- as.numeric(merge.dataset$integrity_4pts)
merge.dataset$swd_post <- as.numeric(merge.dataset$swd_post)
merge.dataset$suspicion <- as.numeric(merge.dataset$suspicion)
merge.dataset$accurate <- as.numeric(merge.dataset$accurate)

View(merge.dataset)

# Create separate data frame for each election

us2012.2020.anes <- merge.dataset %>% filter(election == "us2012-anes" | election == "us2016-anes" | election == "us2020-anes")
us2012.anes <- merge.dataset %>% filter(election == "us2012-anes")
us2016.anes <- merge.dataset %>% filter(election == "us2016-anes")
us2020.anes <- merge.dataset %>% filter(election == "us2020-anes")

# 2012 U.S. presidential election (PID)

model.integrity.3x.us2012.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                     knowledge_n + male + age + white + income + education +
                                     region, data = us2012.anes, weights=weight)

summary(model.integrity.3x.us2012.pid)

tiff("integrity_3x_us2012_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.us2012.pid.g <- plot_model(model.integrity.3x.us2012.pid, type = "eff", 
                                        terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                        title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                        legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.us2012.pid.g$facet$params$nrow=3

integrity.3x.us2012.pid.g <- integrity.3x.us2012.pid.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                               axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                               plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.us2012.pid.g)

dev.off()

# 2012 U.S. presidential election (vote)

model.integrity.3x.us2012.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                      knowledge_n + male + age + white + income + education +
                                      region, data = us2012.anes, weights=weight)

summary(model.integrity.3x.us2012.vote)

tiff("integrity_3x_us2012_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.us2012.vote.g <- plot_model(model.integrity.3x.us2012.vote, type = "eff", 
                                         terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                         title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                         legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.us2012.vote.g$facet$params$nrow=3

integrity.3x.us2012.vote.g <- integrity.3x.us2012.vote.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.us2012.vote.g)

dev.off()

# 2016 U.S. presidential election (PID)

model.integrity.3x.us2016.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                     knowledge_n + male + age + white + income + education +
                                     region, data = us2016.anes, weights=weight)

summary(model.integrity.3x.us2016.pid)

tiff("integrity_3x_us2016_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.us2016.pid.g <- plot_model(model.integrity.3x.us2016.pid, type = "eff", 
                                        terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                        title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                        legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.us2016.pid.g$facet$params$nrow=3

integrity.3x.us2016.pid.g <- integrity.3x.us2016.pid.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                               axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                               plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.us2016.pid.g)

dev.off()

# 2016 U.S. presidential election (vote)

model.integrity.3x.us2016.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                      knowledge_n + male + age + white + income + education +
                                      region, data = us2016.anes, weights=weight)

summary(model.integrity.3x.us2016.vote)

tiff("integrity_3x_us2016_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.us2016.vote.g <- plot_model(model.integrity.3x.us2016.vote, type = "eff", 
                                         terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                         title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                         legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.us2016.vote.g$facet$params$nrow=3

integrity.3x.us2016.vote.g <- integrity.3x.us2016.vote.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.us2016.vote.g)

dev.off()

# 2020 U.S. presidential election (PID)

model.integrity.3x.us2020.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                     knowledge_n + male + age + white + income + education +
                                     region, data = us2020.anes, weights=weight)

summary(model.integrity.3x.us2020.pid)

tiff("integrity_3x_us2020_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.us2020.pid.g <- plot_model(model.integrity.3x.us2020.pid, type = "eff", 
                                        terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                        title = "                 2020 ANES", axis.title = c("", ""), 
                                        legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.us2020.pid.g$facet$params$nrow=3

integrity.3x.us2020.pid.g <- integrity.3x.us2020.pid.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                               axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                               plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.us2020.pid.g)

dev.off()

# 2020 U.S. presidential election (Pre/Post)

model.integrity.3x.pre.us2020.pid = lm(integrity_4pts ~ accurate + pidstatus_whole*unexpected_p*suspicion + 
                                     knowledge_n + male + age + white + income + education +
                                     region, data = us2020.anes, weights=weight)

summary(model.integrity.3x.pre.us2020.pid)

tiff("integrity_3x_pre_us2020_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.pre.us2020.pid.g <- plot_model(model.integrity.3x.pre.us2020.pid, type = "eff", 
                                        terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                        title = "                 2020 ANES", axis.title = c("", ""), 
                                        legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.pre.us2020.pid.g$facet$params$nrow=3

integrity.3x.pre.us2020.pid.g <- integrity.3x.pre.us2020.pid.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                               axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                               plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.pre.us2020.pid.g)

dev.off()

# 2020 U.S. presidential election (vote)

model.integrity.3x.us2020.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion +
                                      knowledge_n + male + age + white + income + education +
                                      region, data = us2020.anes, weights=weight)

summary(model.integrity.3x.us2020.vote)

tiff("integrity_3x_us2020_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.us2020.vote.g <- plot_model(model.integrity.3x.us2020.vote, type = "eff",
                                         terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                         title = "                 2020 ANES", axis.title = c("", ""),
                                         legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.us2020.vote.g$facet$params$nrow=3

integrity.3x.us2020.vote.g <- integrity.3x.us2020.vote.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.us2020.vote.g)

dev.off()


########################################################
## 3. FIGURE 3: THREE-WAY INTERACTION: SUSPICION, SWD ##
########################################################

# Load data

merge.swd <- sjlabelled::read_stata("merge_integrity.dta")

merge.swd$pidstatus_whole <- ordered(merge.swd$pidstatus_whole,
                                     levels = c(1,2,3),
                                     labels = c("Losers", "Independents", "Winners"))

merge.swd$voteactstatus_whole <- ordered(merge.swd$voteactstatus_whole,
                                         levels = c(1,2,3),
                                         labels = c("Losers", "Non-Voters", "Winners"))

merge.swd$unexpected_p <- ordered(merge.swd$unexpected_p,
                                  levels = c(0,1),
                                  labels = c("Expected", "Unexpected"))

merge.swd$unexpected_v <- ordered(merge.swd$unexpected_v,
                                  levels = c(0,1),
                                  labels = c("Expected", "Unexpected"))

# Define variables as factor or numeric

merge.swd$pidstatus_whole <- as.factor(merge.swd$pidstatus_whole)
merge.swd$voteactstatus_whole <- as.factor(merge.swd$voteactstatus_whole)
merge.swd$unexpected_p <- as.factor(merge.swd$unexpected_p)
merge.swd$unexpected_v <- as.factor(merge.swd$unexpected_v)
merge.swd$male <- as.factor(merge.swd$male)
merge.swd$white <- as.factor(merge.swd$white)
merge.swd$education <- as.factor(merge.swd$education)
merge.swd$knowledge_n <- as.numeric(merge.swd$knowledge_n)
merge.swd$income <- as.numeric(merge.swd$income)
merge.swd$region <- as.factor(merge.swd$region)

merge.swd$swd_post <- as.numeric(merge.swd$swd_post)
merge.swd$swd_post <- as.numeric(merge.swd$swd_post)
merge.swd$suspicion <- as.numeric(merge.swd$suspicion)
merge.swd$economy_soc <- as.numeric(merge.swd$economy_soc)
merge.swd$economy_ego <- as.numeric(merge.swd$economy_ego)

View(merge.swd)

# Create separate data frame for each election

us2012.2020.anes <- merge.swd %>% filter(election == "us2012-anes" | election == "us2016-anes" | election == "us2020-anes")
us2012.anes <- merge.swd %>% filter(election == "us2012-anes")
us2016.anes <- merge.swd %>% filter(election == "us2016-anes")
us2020.anes <- merge.swd %>% filter(election == "us2020-anes")

# 2012 U.S. presidential election (PID)

model.swd.3x.us2012.pid = lm(swd_post ~ pidstatus_whole*unexpected_p*suspicion + 
                               economy_soc + economy_ego + knowledge_n + male + age + white + income + education +
                               region, data = us2012.anes, weights=weight)

summary(model.swd.3x.us2012.pid)

tiff("swd_3x_us2012_pid_g.tiff", units="in", width=5, height=7, res=300)

swd.3x.us2012.pid.g <- plot_model(model.swd.3x.us2012.pid, type = "eff", 
                                  terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                  title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                  legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

swd.3x.us2012.pid.g$facet$params$nrow=3

swd.3x.us2012.pid.g <- swd.3x.us2012.pid.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                   axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                   plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(swd.3x.us2012.pid.g)

dev.off()

# 2012 U.S. presidential election (vote)

model.swd.3x.us2012.vote = lm(swd_post ~ voteactstatus_whole*unexpected_v*suspicion + 
                                economy_soc + economy_ego + knowledge_n + male + age + white + income + education +
                                region, data = us2012.anes, weights=weight)

summary(model.swd.3x.us2012.vote)

tiff("swd_3x_us2012_vote_g.tiff", units="in", width=5, height=7, res=300)

swd.3x.us2012.vote.g <- plot_model(model.swd.3x.us2012.vote, type = "eff", 
                                   terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                   title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                   legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

swd.3x.us2012.vote.g$facet$params$nrow=3

swd.3x.us2012.vote.g <- swd.3x.us2012.vote.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                     axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                     plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(swd.3x.us2012.vote.g)

dev.off()

# 2016 U.S. presidential election (PID)

model.swd.3x.us2016.pid = lm(swd_post ~ pidstatus_whole*unexpected_p*suspicion + 
                               economy_soc + economy_ego + knowledge_n + male + age + white + income + education +
                               region, data = us2016.anes, weights=weight)

summary(model.swd.3x.us2016.pid)

tiff("swd_3x_us2016_pid_g.tiff", units="in", width=5, height=7, res=300)

swd.3x.us2016.pid.g <- plot_model(model.swd.3x.us2016.pid, type = "eff", 
                                  terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                  title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                  legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

swd.3x.us2016.pid.g$facet$params$nrow=3

swd.3x.us2016.pid.g <- swd.3x.us2016.pid.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                   axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                   plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(swd.3x.us2016.pid.g)

dev.off()

# 2016 U.S. presidential election (vote)

model.swd.3x.us2016.vote = lm(swd_post ~ voteactstatus_whole*unexpected_v*suspicion + 
                                economy_soc + economy_ego + knowledge_n + male + age + white + income + education +
                                region, data = us2016.anes, weights=weight)

summary(model.swd.3x.us2016.vote)

tiff("swd_3x_us2016_vote_g.tiff", units="in", width=5, height=7, res=300)

swd.3x.us2016.vote.g <- plot_model(model.swd.3x.us2016.vote, type = "eff", 
                                   terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                   title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                   legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

swd.3x.us2016.vote.g$facet$params$nrow=3

swd.3x.us2016.vote.g <- swd.3x.us2016.vote.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                     axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                     plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(swd.3x.us2016.vote.g)

dev.off()

# 2020 U.S. presidential election (PID)

model.swd.3x.us2020.pid = lm(swd_post ~ pidstatus_whole*unexpected_p*suspicion + 
                               economy_soc + economy_ego + knowledge_n + male + age + white + income + education +
                               region, data = us2020.anes, weights=weight)

summary(model.swd.3x.us2020.pid)

tiff("swd_3x_us2020_pid_g.tiff", units="in", width=5, height=7, res=300)

swd.3x.us2020.pid.g <- plot_model(model.swd.3x.us2020.pid, type = "eff", 
                                  terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                  title = "                 2020 ANES", axis.title = c("", ""), 
                                  legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

swd.3x.us2020.pid.g$facet$params$nrow=3

swd.3x.us2020.pid.g <- swd.3x.us2020.pid.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                   axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                   plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(swd.3x.us2020.pid.g)

dev.off()

# 2020 U.S. presidential election (vote)

model.swd.3x.us2020.vote = lm(swd_post ~ voteactstatus_whole*unexpected_v*suspicion +
                                economy_soc + economy_ego + knowledge_n + male + age + white + income + education +
                                region, data = us2020.anes, weights=weight)

summary(model.swd.3x.us2020.vote)

tiff("swd_3x_us2020_vote_g.tiff", units="in", width=5, height=7, res=300)

swd.3x.us2020.vote.g <- plot_model(model.swd.3x.us2020.vote, type = "eff",
                                   terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                   title = "                 2020 ANES", axis.title = c("", ""),
                                   legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

swd.3x.us2020.vote.g$facet$params$nrow=3

swd.3x.us2020.vote.g <- swd.3x.us2020.vote.g + theme(legend.text=element_text(size=12),strip.text.x = element_text(size = 14),
                                                     axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                     plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(swd.3x.us2020.vote.g)

dev.off()



########### CLOSENESS


# 2012 U.S. presidential election (PID)

model.3x.notclose.us2012.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                    knowledge_n + male + age + white + income + education +
                                    region, data = us2012.anes, subset=closeness_whole==0, weights=weight)

summary(model.3x.notclose.us2012.pid)

tiff("integrity_3x_notclose_us2012_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notclose.us2012.pid.g <- plot_model(model.3x.notclose.us2012.pid, type = "eff", 
                                                 terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                                 title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                                 legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.notclose.us2012.pid.g$facet$params$nrow=3

integrity.3x.notclose.us2012.pid.g <- integrity.3x.notclose.us2012.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12))

print(integrity.3x.notclose.us2012.pid.g)

dev.off()

# 2012 U.S. presidential election (vote)

model.3x.notclose.us2012.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                     knowledge_n + male + age + white + income + education +
                                     region, data = us2012.anes, subset=closeness_whole==0, weights=weight)

summary(model.3x.notclose.us2012.vote)

tiff("integrity_3x_notclose_us2012_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notclose.us2012.vote.g <- plot_model(model.3x.notclose.us2012.vote, type = "eff", 
                                                  terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                                  title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                                  legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.notclose.us2012.vote.g$facet$params$nrow=3

integrity.3x.notclose.us2012.vote.g <- integrity.3x.notclose.us2012.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                                   axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                                   plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notclose.us2012.vote.g)

dev.off()

# 2016 U.S. presidential election (PID)

model.3x.notclose.us2016.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                    knowledge_n + male + age + white + income + education +
                                    region, data = us2016.anes, subset=closeness_whole==0, weights=weight)

summary(model.3x.notclose.us2016.pid)

tiff("integrity_3x_notclose_us2016_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notclose.us2016.pid.g <- plot_model(model.3x.notclose.us2016.pid, type = "eff", 
                                                 terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                                 title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                                 legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.notclose.us2016.pid.g$facet$params$nrow=3

integrity.3x.notclose.us2016.pid.g <- integrity.3x.notclose.us2016.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notclose.us2016.pid.g)

dev.off()

# 2016 U.S. presidential election (vote)

model.3x.notclose.us2016.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                     knowledge_n + male + age + white + income + education +
                                     region, data = us2016.anes, subset=closeness_whole==0, weights=weight)

summary(model.3x.notclose.us2016.vote)

tiff("integrity_3x_notclose_us2016_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notclose.us2016.vote.g <- plot_model(model.3x.notclose.us2016.vote, type = "eff", 
                                                  terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                                  title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                                  legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.notclose.us2016.vote.g$facet$params$nrow=3

integrity.3x.notclose.us2016.vote.g <- integrity.3x.notclose.us2016.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                                   axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                                   plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notclose.us2016.vote.g)

dev.off()

# 2020 U.S. presidential election (PID)

model.3x.notclose.us2020.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                    knowledge_n + male + age + white + income + education +
                                    region, data = us2020.anes, subset=closeness_whole==0, weights=weight)

summary(model.3x.notclose.us2020.pid)

tiff("integrity_3x_notclose_us2020_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notclose.us2020.pid.g <- plot_model(model.3x.notclose.us2020.pid, type = "eff", 
terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
title = "Easy Victory", axis.title = c("Suspicion Index", ""), 
legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
axis.text.y = element_text(size = 12))

integrity.3x.notclose.us2020.pid.g$facet$params$nrow=3

integrity.3x.notclose.us2020.pid.g <- integrity.3x.notclose.us2020.pid.g + theme(strip.text.x = element_text(size = 14),
axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
plot.title = element_text(size = 15, hjust = 0.5), legend.title=element_text(size=12), legend.text=element_text(size=12)) 

print(integrity.3x.notclose.us2020.pid.g)

dev.off()

# 2020 U.S. presidential election (vote)

model.3x.notclose.us2020.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion +
                                     knowledge_n + male + age + white + income + education +
                                     region, data = us2020.anes, subset=closeness_whole==0, weights=weight)

summary(model.3x.notclose.us2020.vote)

tiff("integrity_3x_notclose_us2020_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notclose.us2020.vote.g <- plot_model(model.3x.notclose.us2020.vote, type = "eff",
                                                  terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                                  title = "Easy Victory", axis.title = c("", ""),
                                                  legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.notclose.us2020.vote.g$facet$params$nrow=3

integrity.3x.notclose.us2020.vote.g <- integrity.3x.notclose.us2020.vote.g + theme(strip.text.x = element_text(size = 14),
axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
plot.title = element_text(size = 15, hjust = 0.5), legend.title=element_text(size=12), legend.text=element_text(size=12)) 

print(integrity.3x.notclose.us2020.vote.g)

dev.off()


# 2012 U.S. presidential election (PID)

model.3x.close.us2012.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                 knowledge_n + male + age + white + income + education +
                                 region, data = us2012.anes, subset=closeness_whole==1, weights=weight)

summary(model.3x.close.us2012.pid)

tiff("integrity_3x_close_us2012_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.close.us2012.pid.g <- plot_model(model.3x.close.us2012.pid, type = "eff", 
                                              terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                              title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                              legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.close.us2012.pid.g$facet$params$nrow=3

integrity.3x.close.us2012.pid.g <- integrity.3x.close.us2012.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                           axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                           plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.close.us2012.pid.g)

dev.off()

# 2012 U.S. presidential election (vote)

model.3x.close.us2012.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                  knowledge_n + male + age + white + income + education +
                                  region, data = us2012.anes, subset=closeness_whole==1, weights=weight)

summary(model.3x.close.us2012.vote)

tiff("integrity_3x_close_us2012_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.close.us2012.vote.g <- plot_model(model.3x.close.us2012.vote, type = "eff", 
                                               terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                               title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                               legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.close.us2012.vote.g$facet$params$nrow=3

integrity.3x.close.us2012.vote.g <- integrity.3x.close.us2012.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                             axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                             plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.close.us2012.vote.g)

dev.off()

# 2016 U.S. presidential election (PID)

model.3x.close.us2016.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                 knowledge_n + male + age + white + income + education +
                                 region, data = us2016.anes, subset=closeness_whole==1, weights=weight)

summary(model.3x.close.us2016.pid)

tiff("integrity_3x_close_us2016_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.close.us2016.pid.g <- plot_model(model.3x.close.us2016.pid, type = "eff", 
                                              terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                              title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                              legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.close.us2016.pid.g$facet$params$nrow=3

integrity.3x.close.us2016.pid.g <- integrity.3x.close.us2016.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                           axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                           plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.close.us2016.pid.g)

dev.off()

# 2016 U.S. presidential election (vote)

model.3x.close.us2016.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                  knowledge_n + male + age + white + income + education +
                                  region, data = us2016.anes, subset=closeness_whole==1, weights=weight)

summary(model.3x.close.us2016.vote)

tiff("integrity_3x_close_us2016_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.close.us2016.vote.g <- plot_model(model.3x.close.us2016.vote, type = "eff", 
                                               terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                               title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                               legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.close.us2016.vote.g$facet$params$nrow=3

integrity.3x.close.us2016.vote.g <- integrity.3x.close.us2016.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                             axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                             plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.close.us2016.vote.g)

dev.off()

# 2020 U.S. presidential election (PID)

model.3x.close.us2020.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                 knowledge_n + male + age + white + income + education +
                                 region, data = us2020.anes, subset=closeness_whole==1, weights=weight)

summary(model.3x.close.us2020.pid)

tiff("integrity_3x_close_us2020_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.close.us2020.pid.g <- plot_model(model.3x.close.us2020.pid, type = "eff", 
                                              terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                              title = "Close Election", axis.title = c("Suspicion Index", "Linear Prediction"), 
                                              legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.close.us2020.pid.g$facet$params$nrow=3

integrity.3x.close.us2020.pid.g <- integrity.3x.close.us2020.pid.g + theme(strip.text.x = element_text(size = 14),
axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
plot.title = element_text(size = 15, hjust = 0.5), legend.title=element_text(size=12),legend.text = element_text(size=12)) 

print(integrity.3x.close.us2020.pid.g)

dev.off()

# 2020 U.S. presidential election (vote)

model.3x.close.us2020.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion +
                                  knowledge_n + male + age + white + income + education +
                                  region, data = us2020.anes, subset=closeness_whole==1, weights=weight)

summary(model.3x.close.us2020.vote)

tiff("integrity_3x_close_us2020_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.close.us2020.vote.g <- plot_model(model.3x.close.us2020.vote, type = "eff",
                                               terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                               title = "Close Election", axis.title = c("", ""),
                                               legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.close.us2020.vote.g$facet$params$nrow=3

integrity.3x.close.us2020.vote.g <- integrity.3x.close.us2020.vote.g + theme(strip.text.x = element_text(size = 14),
axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
plot.title = element_text(size = 15, hjust = 0.5), legend.title=element_text(size=12),legend.text = element_text(size=12)) 

print(integrity.3x.close.us2020.vote.g)

dev.off()

# 2012 U.S. presidential election (PID)

model.3x.care.us2012.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                knowledge_n + male + age + white + income + education +
                                region, data = us2012.anes, subset=care_whole==1, weights=weight)

summary(model.3x.care.us2012.pid)

tiff("integrity_3x_care_us2012_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.care.us2012.pid.g <- plot_model(model.3x.care.us2012.pid, type = "eff", 
                                             terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                             title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                             legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.care.us2012.pid.g$facet$params$nrow=3

integrity.3x.care.us2012.pid.g <- integrity.3x.care.us2012.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                         axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                         plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.care.us2012.pid.g)

dev.off()

# 2012 U.S. presidential election (vote)

model.3x.care.us2012.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                 knowledge_n + male + age + white + income + education +
                                 region, data = us2012.anes, subset=care_whole==1, weights=weight)

summary(model.3x.care.us2012.vote)

tiff("integrity_3x_care_us2012_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.care.us2012.vote.g <- plot_model(model.3x.care.us2012.vote, type = "eff", 
                                              terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                              title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                              legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.care.us2012.vote.g$facet$params$nrow=3

integrity.3x.care.us2012.vote.g <- integrity.3x.care.us2012.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                           axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                           plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.care.us2012.vote.g)

dev.off()

# 2016 U.S. presidential election (PID)

model.3x.care.us2016.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                knowledge_n + male + age + white + income + education +
                                region, data = us2016.anes, subset=care_whole==1, weights=weight)

summary(model.3x.care.us2016.pid)

tiff("integrity_3x_care_us2016_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.care.us2016.pid.g <- plot_model(model.3x.care.us2016.pid, type = "eff", 
                                             terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                             title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                             legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.care.us2016.pid.g$facet$params$nrow=3

integrity.3x.care.us2016.pid.g <- integrity.3x.care.us2016.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                         axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                         plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.care.us2016.pid.g)

dev.off()

# 2016 U.S. presidential election (vote)

model.3x.care.us2016.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                 knowledge_n + male + age + white + income + education +
                                 region, data = us2016.anes, subset=care_whole==1, weights=weight)

summary(model.3x.care.us2016.vote)

tiff("integrity_3x_care_us2016_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.care.us2016.vote.g <- plot_model(model.3x.care.us2016.vote, type = "eff", 
                                              terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                              title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                              legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.care.us2016.vote.g$facet$params$nrow=3

integrity.3x.care.us2016.vote.g <- integrity.3x.care.us2016.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                           axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                           plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.care.us2016.vote.g)

dev.off()

# 2020 U.S. presidential election (PID)

model.3x.care.us2020.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                knowledge_n + male + age + white + income + education +
                                region, data = us2020.anes, subset=care_whole==1, weights=weight)

summary(model.3x.care.us2020.pid)

tiff("integrity_3x_care_us2020_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.care.us2020.pid.g <- plot_model(model.3x.care.us2020.pid, type = "eff", 
                                             terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                             title = "          Care About Outcome", axis.title = c("Suspicion Index", ""), 
                                             legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.care.us2020.pid.g$facet$params$nrow=3

integrity.3x.care.us2020.pid.g <- integrity.3x.care.us2020.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                         axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                         plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.care.us2020.pid.g)

dev.off()

# 2020 U.S. presidential election (vote)

model.3x.care.us2020.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion +
                                 knowledge_n + male + age + white + income + education +
                                 region, data = us2020.anes, subset=care_whole==1, weights=weight)

summary(model.3x.care.us2020.vote)

tiff("integrity_3x_care_us2020_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.care.us2020.vote.g <- plot_model(model.3x.care.us2020.vote, type = "eff",
                                              terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                              title = "                 2020 ANES", axis.title = c("", ""),
                                              legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.care.us2020.vote.g$facet$params$nrow=3

integrity.3x.care.us2020.vote.g <- integrity.3x.care.us2020.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                           axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                           plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.care.us2020.vote.g)

dev.off()


# 2012 U.S. presidential election (PID)

model.3x.notcare.us2012.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                   knowledge_n + male + age + white + income + education +
                                   region, data = us2012.anes, subset=care_whole==0, weights=weight)

summary(model.3x.notcare.us2012.pid)

tiff("integrity_3x_notcare_us2012_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notcare.us2012.pid.g <- plot_model(model.3x.notcare.us2012.pid, type = "eff", 
                                                terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                                title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                                legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.notcare.us2012.pid.g$facet$params$nrow=3

integrity.3x.notcare.us2012.pid.g <- integrity.3x.notcare.us2012.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                               axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                               plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notcare.us2012.pid.g)

dev.off()

# 2012 U.S. presidential election (vote)

model.3x.notcare.us2012.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                    knowledge_n + male + age + white + income + education +
                                    region, data = us2012.anes, subset=care_whole==0, weights=weight)

summary(model.3x.notcare.us2012.vote)

tiff("integrity_3x_notcare_us2012_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notcare.us2012.vote.g <- plot_model(model.3x.notcare.us2012.vote, type = "eff", 
                                                 terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                                 title = "                 2012 ANES", axis.title = c("", "Linear Prediction"), 
                                                 legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.text.x = element_text(size = 12), axis.text.y = element_text(size = 12))

integrity.3x.notcare.us2012.vote.g$facet$params$nrow=3

integrity.3x.notcare.us2012.vote.g <- integrity.3x.notcare.us2012.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notcare.us2012.vote.g)

dev.off()

# 2016 U.S. presidential election (PID)

model.3x.notcare.us2016.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                   knowledge_n + male + age + white + income + education +
                                   region, data = us2016.anes, subset=care_whole==0, weights=weight)

summary(model.3x.notcare.us2016.pid)

tiff("integrity_3x_notcare_us2016_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notcare.us2016.pid.g <- plot_model(model.3x.notcare.us2016.pid, type = "eff", 
                                                terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                                title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                                legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.notcare.us2016.pid.g$facet$params$nrow=3

integrity.3x.notcare.us2016.pid.g <- integrity.3x.notcare.us2016.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                               axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                               plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notcare.us2016.pid.g)

dev.off()

# 2016 U.S. presidential election (vote)

model.3x.notcare.us2016.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion + 
                                    knowledge_n + male + age + white + income + education +
                                    region, data = us2016.anes, subset=care_whole==0, weights=weight)

summary(model.3x.notcare.us2016.vote)

tiff("integrity_3x_notcare_us2016_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notcare.us2016.vote.g <- plot_model(model.3x.notcare.us2016.vote, type = "eff", 
                                                 terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                                 title = "                 2016 ANES", axis.title = c("Suspicion Index", ""), 
                                                 legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.notcare.us2016.vote.g$facet$params$nrow=3

integrity.3x.notcare.us2016.vote.g <- integrity.3x.notcare.us2016.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notcare.us2016.vote.g)

dev.off()

# 2020 U.S. presidential election (PID)

model.3x.notcare.us2020.pid = lm(integrity_4pts ~ pidstatus_whole*unexpected_p*suspicion + 
                                   knowledge_n + male + age + white + income + education +
                                   region, data = us2020.anes, subset=care_whole==0, weights=weight)

summary(model.3x.notcare.us2020.pid)

tiff("integrity_3x_notcare_us2020_pid_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notcare.us2020.pid.g <- plot_model(model.3x.notcare.us2020.pid, type = "eff", 
                                                terms = c("suspicion [0,1]", "unexpected_p", "pidstatus_whole"), 
                                                title = "                 Do Not Care", axis.title = c("Suspicion Index", "Linear Prediction"), 
                                                legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14), 
                                  axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.notcare.us2020.pid.g$facet$params$nrow=3

integrity.3x.notcare.us2020.pid.g <- integrity.3x.notcare.us2020.pid.g + theme(strip.text.x = element_text(size = 14),
                                                                               axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                               plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notcare.us2020.pid.g)

dev.off()

# 2020 U.S. presidential election (vote)

model.3x.notcare.us2020.vote = lm(integrity_4pts ~ voteactstatus_whole*unexpected_v*suspicion +
                                    knowledge_n + male + age + white + income + education +
                                    region, data = us2020.anes, subset=care_whole==0, weights=weight)

summary(model.3x.notcare.us2020.vote)

tiff("integrity_3x_notcare_us2020_vote_g.tiff", units="in", width=5, height=7, res=300)

integrity.3x.notcare.us2020.vote.g <- plot_model(model.3x.notcare.us2020.vote, type = "eff",
                                                 terms = c("suspicion [0,1]", "unexpected_v", "voteactstatus_whole"), 
                                                 title = "                 2020 ANES", axis.title = c("", ""),
                                                 legend.title = "Unexpected?") + scale_color_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) + scale_fill_manual(labels = c("No", "Yes"), values = c('#00BFC4','#F8766D')) +
  xlim(0,1) + ylim(1,4.5) + theme(strip.text.x = element_text(size = 14),
                                  axis.ticks.y = element_blank(), axis.text.x = element_text(size = 12), 
                                  axis.text.y = element_text(size = 12))

integrity.3x.notcare.us2020.vote.g$facet$params$nrow=3

integrity.3x.notcare.us2020.vote.g <- integrity.3x.notcare.us2020.vote.g + theme(strip.text.x = element_text(size = 14),
                                                                                 axis.title.y = element_text(size=14), axis.title.x = element_text(size=14), 
                                                                                 plot.title = element_text(size = 15), legend.title=element_text(size=12)) 

print(integrity.3x.notcare.us2020.vote.g)

dev.off()



###############################################
## 4. FIGURE 4: PERCEIVED ELECTION CLOSENESS ##
###############################################

# Load data (PID & Perceived electoral integrity)

integrity.closeness.pid <- read_dta("parmest_inter_integrity_closeness_pid.dta")

View(integrity.closeness.pid)

integrity.closeness.pid <- integrity.closeness.pid %>% filter(!(electionid == "us1996-2004" | 
                                                              electionid == "us2012-2020"))

integrity.closeness.pid <- integrity.closeness.pid %>% mutate(comp = dplyr::recode(comp, "independents" = "nopid"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("integrity_closeness_pid_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.closeness.pid.g <- ggplot(data = integrity.closeness.pid, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = close, shape = close),
position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.closeness.pid.g <- integrity.closeness.pid.g + scale_colour_manual(limits=c("close", "notclose"),values=c("#F8766D", "#00BA38"), 
name="Closeness", labels=c("Close","Not Close")) +
  scale_shape_manual(limits=c("close", "notclose"),values=c(15,16), name="Closeness", labels=c("Close","Not Close")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
"US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
"nopid" = "Independents", "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.closeness.pid.g$facet$params$nrow=1

print(integrity.closeness.pid.g)

dev.off()

# Load data (PID & Satisfaction with democracy)

swd.closeness.pid <- read_dta("parmest_inter_swd_closeness_pid.dta")

View(swd.closeness.pid)

swd.closeness.pid <- swd.closeness.pid %>% filter(!(electionid == "us1996-2004" | 
                                                                  electionid == "us2012-2020"))

swd.closeness.pid <- swd.closeness.pid %>% mutate(comp = dplyr::recode(comp, "independents" = "nopid"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("swd_closeness_pid_g.png", width = 175, height = 100, units='mm', res = 300)

swd.closeness.pid.g <- ggplot(data = swd.closeness.pid, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = close, shape = close),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
linetype="dashed", color = "dimgray", linewidth=0.5) 

swd.closeness.pid.g <- swd.closeness.pid.g + scale_colour_manual(limits=c("close", "notclose"),values=c("#F8766D", "#00BA38"), 
name="Closeness", labels=c("Close","Not Close")) +
  scale_shape_manual(limits=c("close", "notclose"),values=c(15,16), name="Closeness", labels=c("Close","Not Close")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
"US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
"nopid" = "Independents", "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

swd.closeness.pid.g$facet$params$nrow=1

print(swd.closeness.pid.g)

dev.off()

# Load data (Vote & Perceived electoral integrity)

integrity.closeness.vote <- read_dta("parmest_inter_integrity_closeness_vote.dta")

View(integrity.closeness.vote)

integrity.closeness.vote <- integrity.closeness.vote %>% filter(!(electionid == "us1996-2004" | 
                                                                    electionid == "us2012-2020"))

integrity.closeness.vote <- integrity.closeness.vote %>% mutate(comp = dplyr::recode(comp, "independents" = "nopid"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("integrity_closeness_vote_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.closeness.vote.g <- ggplot(data = integrity.closeness.vote, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = close, shape = close),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                                                                               linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.closeness.vote.g <- integrity.closeness.vote.g + scale_colour_manual(limits=c("close", "notclose"),values=c("#F8766D", "#00BA38"), 
                                                                               name="Closeness", labels=c("Close","Not Close")) +
  scale_shape_manual(limits=c("close", "notclose"),values=c(15,16), name="Closeness", labels=c("Close","Not Close")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                                                                              limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                                                                                                                                                               "US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
                     breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
                                                                                                   "nonvoters" = "Non-Voters", "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                                                                                                                                                            axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.closeness.vote.g$facet$params$nrow=1

print(integrity.closeness.vote.g)

dev.off()

# Load data (Vote & Satisfaction with democracy)

swd.closeness.vote <- read_dta("parmest_inter_swd_closeness_vote.dta")

View(swd.closeness.vote)

swd.closeness.vote <- swd.closeness.vote %>% filter(!(electionid == "us1996-2004" | 
                                                        electionid == "us2012-2020"))

swd.closeness.vote <- swd.closeness.vote %>% mutate(comp = dplyr::recode(comp, "independents" = "nopid"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("swd_closeness_vote_g.png", width = 175, height = 100, units='mm', res = 300)

swd.closeness.vote.g <- ggplot(data = swd.closeness.vote, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = close, shape = close),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                                                                               linetype="dashed", color = "dimgray", linewidth=0.5) 

swd.closeness.vote.g <- swd.closeness.vote.g + scale_colour_manual(limits=c("close", "notclose"),values=c("#F8766D", "#00BA38"), 
                                                                   name="Closeness", labels=c("Close","Not Close")) +
  scale_shape_manual(limits=c("close", "notclose"),values=c(15,16), name="Closeness", labels=c("Close","Not Close")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                                                                              limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                                                                                                                                                               "US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
                     breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
                                                                                                   "nonvoters" = "Non-Voters", "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                                                                                                                                                            axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

swd.closeness.vote.g$facet$params$nrow=1

print(swd.closeness.vote.g)

dev.off()


##################
## PID STRENGTH ##
##################

# Load data (PID & Perceived electoral integrity)

integrity.strength.pid <- read_dta("parmest_inter_integrity_strength_pid.dta")

View(integrity.strength.pid)

integrity.strength.pid <- integrity.strength.pid %>% filter(!(electionid == "us1996-2004" | 
                                                                electionid == "us2012-2020"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("integrity_strength_pid_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.strength.pid.g <- ggplot(data = integrity.strength.pid, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = strength, shape = strength),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                                                                               linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.strength.pid.g <- integrity.strength.pid.g + scale_colour_manual(limits=c("weak", "strong"),values=c("#F8766D", "#00BA38"), 
                                                                           name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  scale_shape_manual(limits=c("weak", "strong"),values=c(15,16), name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                                                                              limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                                                                                                                                                               "US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
                     breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
                                                                                                   "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                                                                                                                                    axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.strength.pid.g$facet$params$nrow=1

print(integrity.strength.pid.g)

dev.off()

# Load data (PID & Satisfaction with democracy)

swd.strength.pid <- read_dta("parmest_inter_swd_strength_pid.dta")

View(swd.strength.pid)

swd.strength.pid <- swd.strength.pid %>% filter(!(electionid == "us1996-2004" | 
                                                    electionid == "us2012-2020"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("swd_strength_pid_g.png", width = 175, height = 100, units='mm', res = 300)

swd.strength.pid.g <- ggplot(data = swd.strength.pid, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = strength, shape = strength),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                                                                               linetype="dashed", color = "dimgray", linewidth=0.5) 

swd.strength.pid.g <- swd.strength.pid.g + scale_colour_manual(limits=c("weak", "strong"),values=c("#F8766D", "#00BA38"), 
                                                               name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  scale_shape_manual(limits=c("weak", "strong"),values=c(15,16), name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                                                                              limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                                                                                                                                                               "US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
                     breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
                                                                                                   "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                                                                                                                                    axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

swd.strength.pid.g$facet$params$nrow=1

print(swd.strength.pid.g)

dev.off()

# Load data (Vote & Perceived electoral integrity)

integrity.strength.vote <- read_dta("parmest_inter_integrity_strength_vote.dta")

View(integrity.strength.vote)

integrity.strength.vote <- integrity.strength.vote %>% filter(!(electionid == "us1996-2004" | 
                                                                  electionid == "us2012-2020"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("integrity_strength_vote_g.png", width = 175, height = 100, units='mm', res = 300)

integrity.strength.vote.g <- ggplot(data = integrity.strength.vote, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = strength, shape = strength),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                                                                               linetype="dashed", color = "dimgray", linewidth=0.5) 

integrity.strength.vote.g <- integrity.strength.vote.g + scale_colour_manual(limits=c("weak", "strong"),values=c("#F8766D", "#00BA38"), 
                                                                             name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  scale_shape_manual(limits=c("weak", "strong"),values=c(15,16), name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                                                                              limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                                                                                                                                                               "US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
                     breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
                                                                                                   "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                                                                                                                                    axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

integrity.strength.vote.g$facet$params$nrow=1

print(integrity.strength.vote.g)

dev.off()

# Load data (Vote & Satisfaction with democracy)

swd.strength.vote <- read_dta("parmest_inter_swd_strength_vote.dta")

View(swd.strength.vote)

swd.strength.vote <- swd.strength.vote %>% filter(!(electionid == "us1996-2004" | 
                                                      electionid == "us2012-2020"))

# Create graph: Unexpected–Expected Gap

theme_set(theme_bw())

png("swd_strength_vote_g.png", width = 175, height = 100, units='mm', res = 300)

swd.strength.vote.g <- ggplot(data = swd.strength.vote, aes(x = estimate, y = electionid)) + 
  geom_effect(show.legend = TRUE, ggplot2::aes(xmin = min95, xmax = max95, colour = strength, shape = strength),
              position = ggstance::position_dodgev(height = 0.5)) + geom_vline(xintercept = 0, 
                                                                               linetype="dashed", color = "dimgray", linewidth=0.5) 

swd.strength.vote.g <- swd.strength.vote.g + scale_colour_manual(limits=c("weak", "strong"),values=c("#F8766D", "#00BA38"), 
                                                                 name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  scale_shape_manual(limits=c("weak", "strong"),values=c(15,16), name="PID Strength", labels=c("Weak/Leaners","Strong")) +
  theme(plot.title = element_text(size = 11, hjust = 0.5)) + scale_y_discrete(name = "", 
                                                                              limits = c("us1996", "us2000", "us2004", "us2012", "us2016", "us2020"), labels=c("US 1996","US 2000","US 2004",
                                                                                                                                                               "US 2012","US 2016","US 2020")) +
  scale_x_continuous(name = "Unexpected–Expected Gap", limits=c(-3, 3), 
                     breaks = seq(-3, 3, 3)) + facet_wrap(vars(comp), labeller = labeller(comp = c("losers" = "Losers",
                                                                                                   "winners" = "Winners"))) + theme(axis.text = element_text(size = 10), axis.text.x = element_text(size = 10),
                                                                                                                                    axis.title = element_text(size = 10), strip.text = element_text(size = 10), legend.title = element_text(size = 9)) + 
  guides(color = guide_legend(override.aes = list(size = 2))) + scale_fill_hue() + ggtitle("")

swd.strength.vote.g$facet$params$nrow=1

print(swd.strength.vote.g)

dev.off()


############################
## DESCRIPTIVE STATISTICS ##
############################

library(expss)

stats <- read_stata("merge_integrity.dta")

stats <- stats %>% filter(sampleS == 1)

View(stats)

stats$pidstatus_whole <- as.factor(stats$pidstatus_whole)
stats$voteactstatus_whole <- as.factor(stats$voteactstatus_whole)
stats$unexpected_p <- as.factor(stats$unexpected_p)
stats$unexpected_v <- as.factor(stats$unexpected_v)
stats$male <- as.factor(stats$male)
stats$education <- as.numeric(stats$education)
stats$knowledge_n <- as.numeric(stats$knowledge_n)
stats$income <- as.numeric(stats$income)
stats$region <- as.factor(stats$region)
stats$integrity_4pts <- as.factor(stats$integrity_4pts)
stats$swd_post <- as.factor(stats$swd_post)

stats$suspicion <- as.numeric(stats$suspicion)
stats$economy_soc <- as.numeric(stats$economy_soc)
stats$economy_ego <- as.numeric(stats$economy_ego)

stats = apply_labels(stats,
swd_post = "Satisfaction with democracy")

stats = apply_labels(stats,
integrity_4pts = "Perceived electoral integrity")

stats = apply_labels(stats,
pidstatus_whole = "PID status")

stats = apply_labels(stats,
education = "Education")

stats = apply_labels(stats,
unexpected_p = "Unexpected outcome")

stats = apply_labels(stats,
suspicion = "Suspicion index")

stats = apply_labels(stats,
economy_soc = "Economy (sociotropic)")

stats = apply_labels(stats,
economy_ego = "Economy (egotropic)")

stats = apply_labels(stats,
knowledge_n = "Political knowledge")

stats = apply_labels(stats,
male = "Gender")

stats = apply_labels(stats,
age = "Age")

stats = apply_labels(stats,
income = "Household income")

tab1 <- table1::table1(~swd_post + pidstatus_whole + unexpected_p + suspicion + 
knowledge_n + economy_soc + economy_ego + male + age + white + income + education | election, data = stats, na.rm = TRUE, digits = 3, format.number = TRUE)

t1flex(tab1) %>% save_as_docx(path="tab1.docx")

tab2 <- table1::table1(~integrity_4pts + pidstatus_whole + unexpected_p + suspicion + 
knowledge_n + male + age + white + income + education | election, data = stats, na.rm = TRUE, digits = 3, format.number = TRUE)

t1flex(tab2) %>% save_as_docx(path="tab2.docx")

